import { getResultAPI, getSuggestionAPI } from '@/api/search';
import { AppDispatch } from '..';

export const getSugestionAction = (q: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getSuggestionAPI(q);
    dispatch({ type: 'search/suggestion', payload: res.data.options });
  };
};

export const clearSugestionAction = () => {
  return { type: 'search/suggestion', payload: [] };
};

export const savekeywordAction = (keyword: string) => {
  return { type: 'search/keyword', payload: keyword };
};
export const clearkeywordAction = () => {
  return { type: 'search/clearkeyword' };
};

export const getResultAction = (q: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getResultAPI({ q });
    dispatch({ type: 'search/saveResult', payload: res.data });
  };
};

export const updateResultsAction = (q: string, page: number, per_page = 10) => {
  return async (dispatch: AppDispatch) => {
    const res = await getResultAPI({ q, page, per_page });
    dispatch({ type: 'search/saveResultsList', payload: res.data });
  };
};
